package com.tsl.reggie.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.UUID;

/**
 * 阿里云 OSS 工具类
 */
@Component
public class AliOSSUtils {
/*
    @Value("${aliyun.oss.endpoint}")
    private String endpoint ;
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId ;
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret ;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName ;
*/
    @Autowired
    AliOssProperties aliOssProperties;

    /**
     * 实现上传图片到OSS
     */
    public String upload(MultipartFile file) throws IOException {
        /*String endpoint=aliOssProperties.getEndpoint();
        String accessKeyId=aliOssProperties.getAccessKeyId();
        String accessKeySecret=aliOssProperties.getAccessKeySecret();
        String bucketName=aliOssProperties.getBucketName();*/

        // 获取上传的文件的输入流
        InputStream inputStream = file.getInputStream();
        // 避免文件覆盖
        String originalFilename = file.getOriginalFilename();
        String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        File imgs = new File("D:/image/"+fileName);

        file.transferTo(imgs);
        //上传文件到 OSS
        /*OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, fileName, inputStream);*/

        //文件访问路径
/*
        String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;
*/
        // 关闭ossClient
        /*ossClient.shutdown();*/
        return fileName;// 把上传到oss的路径返回
    }
    public  void downLoad(String filename, HttpServletResponse response) throws Exception {
        //通过输入流读取文件
        FileInputStream fileInputStream =new FileInputStream("D:/image/"+filename);
        ServletOutputStream outputStream = response.getOutputStream();
        response.setContentType("image/jpeg");
        byte[] bytes = new byte[1024];
        Integer len ;
        //通过respond输出给浏览器
        while((len = fileInputStream.read(bytes)) != -1){
            outputStream.write(bytes,0,len);
        }
        //关闭所有流
        outputStream.close();
        fileInputStream.close();

        /*String endpoint=aliOssProperties.getEndpoint();
        String accessKeyId=aliOssProperties.getAccessKeyId();
        String accessKeySecret=aliOssProperties.getAccessKeySecret();
        String bucketName=aliOssProperties.getBucketName();

        response.setContentType("image/jpeg");
       //文件名如果包含中文需要指定编码
        String objectName = filename;

        OutputStream outputStream = response.getOutputStream();

        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        OSSObject ossObject = ossClient.getObject(bucketName, objectName);
        //读取内容
        InputStream inputStream = ossObject.getObjectContent();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }
        inputStream.close();
        ossObject.close();
        ossClient.shutdown();

        outputStream.flush(); // 刷新输出流
        outputStream.close(); // 关闭输出流*/
    }

}
